﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using UnityEditor;
using UnityEngine;

namespace DashFire.Assetbundle
{
  internal class ExportRes_UI
  {
    static string[] s_ResExcludePattern = new string[] { ".cs", ".js", ".dll", ".a" };
    static string s_UIFontZipFile = "ui_font.ab";
    static string s_UIFontResName = "ui_font";
    static string s_UISoundZipFile = "ui_sound.ab";
    static string s_UISoundResName = "ui_sound";
    static string s_UIShaderZipFile = "ui_shader.ab";
    static string s_UIShaderResName = "ui_shader";

    static HashSet<string> s_AtlasAssetsContent = new HashSet<string>();
    static HashSet<string> s_TextureAssetsContent = new HashSet<string>();
    static HashSet<string> s_UIAssetsContent = new HashSet<string>();
    static bool s_IsFontInContent = false;
    static bool s_IsSoundInContent = false;
    static bool s_IsShaderInContent = false;

    static ExportResInfo s_UIFontInfo = null;
    static ExportResInfo s_UISoundInfo = null;
    static ExportResInfo s_UIShaderInfo = null;
    static List<ExportResInfo> s_UIInfo = new List<ExportResInfo>();
    static List<ExportResInfo> s_UIAtlasesInfo = new List<ExportResInfo>();

    internal static bool ExportClientResources()
    {
      if (!ExportConfig.s_IsSupportUIUpdage) {
        ExportConfig.Log("ExportRes_UI.ExportClientResources Support Closed.");
        return true;
      }

      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;

      // 1.Init Environment
      if (!InitEnvironment()) {
        ExportConfig.Log("ExportDiffResources.InitEnvironment failed:");
        return false;
      }

      // 2.Generate ui dependency tree
      SearchUIFont();
      SearchUISound();
      SearchUIShader();
      SearchAssets("ui_atlases");
      SearchAssets("ui_texture");
      SearchAssets("ui");

      // 3.Collect ui res to build
      foreach (ExportResInfo info in tExportResDict.Values) {
        if (info.m_Type == "ui_shader") {
          s_IsShaderInContent = true;
          info.m_IsNeedBuild = true;
        } else if (info.m_Type == "ui_font") {
          s_IsFontInContent = true;
          info.m_IsNeedBuild = true;
        } else if (info.m_Type == "ui_sound") {
          s_IsSoundInContent = true;
          info.m_IsNeedBuild = true;
        } else if (info.m_Type == "ui_atlases") {
          s_AtlasAssetsContent.Add(info.m_AssetName);
          info.m_IsNeedBuild = true;
        } else if (info.m_Type == "ui_texture") {
          s_TextureAssetsContent.Add(info.m_AssetName);
          info.m_IsNeedBuild = true;
        } else if (info.m_Type == "ui") {
          s_UIAssetsContent.Add(info.m_AssetName);
          info.m_IsNeedBuild = true;
        }
      }

      // 4.Export all res
      ExportAsConfig();

      ExportConfig.Log("ExportRes_UI.ExportClientResources Success");
      return true;
    }
    internal static bool ExportDiffResources()
    {
      if (!ExportConfig.s_IsSupportUIUpdage) {
        ExportConfig.Log("ExportRes_UI.ExportClientResources Support Closed.");
        return true;
      }

      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;
      List<string> tDiffAssetList = new List<string>();

      // 1.Init Environment
      if (!InitEnvironment()) {
        ExportConfig.Log("ExportDiffResources.InitEnvironment failed:");
        return false;
      }

      // 2.Collect ui res to build
      SearchUIFont();
      SearchUISound();
      SearchUIShader();
      SearchAssets("ui_atlases");
      SearchAssets("ui_texture");
      SearchAssets("ui");

      // 3.Collect solo res g
      string tmpGuid = string.Empty;
      foreach (string assetFile in ExportConfig.s_DiffContent) {
        string assetFileLower = assetFile.ToLower();
        if (!string.IsNullOrEmpty(assetFileLower)
          && !ExportConfig.IsEndWith(assetFile, s_ResExcludePattern)
          ) {
          if (File.Exists(assetFileLower)) {
            if (ExportConfig.CheckAssetType(assetFileLower, "ui_shader")) {
              s_IsShaderInContent = true;
              ExportConfig.Log("CheckDiffFile NeedBuild:{0} DependGuid:{1}", "ui_shader", assetFileLower);
            } else if (ExportConfig.CheckAssetType(assetFileLower, "ui_font")) {
              s_IsFontInContent = true;
              ExportConfig.Log("CheckDiffFile NeedBuild:{0} DependGuid:{1}", "ui_font", assetFileLower);
            } else if (ExportConfig.CheckAssetType(assetFileLower, "ui_sound")) {
              s_IsSoundInContent = true;
              ExportConfig.Log("CheckDiffFile NeedBuild:{0} DependGuid:{1}", "ui_sound", assetFileLower);
            } else {
              tmpGuid = AssetDatabase.AssetPathToGUID(assetFileLower);
              if (!string.IsNullOrEmpty(tmpGuid)) {
                tDiffAssetList.Add(tmpGuid);
              }
            }
          }
        }
      }

      // 4.Check ExportResInfo "m_IsNeedBuild" variable
      foreach (ExportResInfo info in tExportResDict.Values) {
        foreach (string diffItem in tDiffAssetList) {
          if (info != null
            && info.m_DependGuid.Contains(diffItem)
            && !string.IsNullOrEmpty(diffItem.Trim())) {
            if (info.m_Type == "ui_atlases") {
              s_AtlasAssetsContent.Add(info.m_AssetName);
              info.m_IsNeedBuild = true;
              ExportConfig.Log("CheckDiffFile NeedBuild:{0} DependGuid:{1}", info.m_AssetName, diffItem);
            } else if (info.m_Type == "ui_texture") {
              s_TextureAssetsContent.Add(info.m_AssetName);
              info.m_IsNeedBuild = true;
              ExportConfig.Log("CheckDiffFile NeedBuild:{0} DependGuid:{1}", info.m_AssetName, diffItem);
            } else if (info.m_Type == "ui") {
              s_UIAssetsContent.Add(info.m_AssetName);
              info.m_IsNeedBuild = true;
              ExportConfig.Log("CheckDiffFile NeedBuild:{0} DependGuid:{1}", info.m_AssetName, diffItem);
            }
          }
        }
      }

      foreach (string resPath in s_UIAssetsContent) {
        ExportResInfo data = tExportResDict[resPath.ToLower()];
        if (data != null && !string.IsNullOrEmpty(data.m_LoadDependGuid)) {
          string[] loadDependGuid = data.m_LoadDependGuid.Split(ExportConfig.s_ConfigSplit, StringSplitOptions.RemoveEmptyEntries);
          foreach (string guid in loadDependGuid) {
            if (guid == "ui_shader") {
              s_IsShaderInContent = true;
            }
            if (guid == "ui_font") {
              s_IsFontInContent = true;
            }
            if (guid == "ui_sound") {
              s_IsSoundInContent = true;
            }
            foreach (ExportResInfo info in s_UIAtlasesInfo) {
              if (guid == info.m_Guid) {
                s_IsShaderInContent = true;
                s_AtlasAssetsContent.Add(info.m_AssetName);
              }
            }
          }
        }
      }

      // 5.Export all res
      ExportAsConfig();

      ExportConfig.Log("ExportRes_UI.ExportSelectedResources Success");
      return true;
    }
    internal static bool ExportSelectedResources(string restype)
    {
      if (!ExportConfig.s_IsSupportUIUpdage) {
        ExportConfig.Log("ExportRes_UI.ExportClientResources Support Closed.");
        return true;
      }

      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;

      // 1.Init Environment
      if (!InitEnvironment()) {
        ExportConfig.Log("ExportDiffResources.InitEnvironment failed:");
        return false;
      }

      // 2.Collect ui res to build
      if (restype == "ui") {
        SearchUIFont();
        SearchUISound();
        SearchUIShader();
        SearchAssets("ui_atlases");
        SearchAssets("ui_texture");
        SearchAssets("ui");

        if (Selection.objects != null && Selection.objects.Length > 0) {
          foreach (UnityEngine.Object obj in Selection.objects) {
            string selectedFile = AssetDatabase.GetAssetPath(obj);
            if (selectedFile != null && !string.IsNullOrEmpty(selectedFile.Trim())) {
              if (File.Exists(selectedFile)) {
                s_UIAssetsContent.Add(selectedFile);
              }
            }
          }
        }
        foreach (string resPath in s_UIAssetsContent) {
          ExportResInfo data = tExportResDict[resPath.ToLower()];
          if (data != null && !string.IsNullOrEmpty(data.m_LoadDependGuid)) {
            string[] loadDependGuid = data.m_LoadDependGuid.Split(ExportConfig.s_ConfigSplit, StringSplitOptions.RemoveEmptyEntries);
            foreach (string guid in loadDependGuid) {
              if (guid == "ui_shader") {
                s_IsShaderInContent = true;
              }
              if (guid == "ui_font") {
                s_IsFontInContent = true;
              }
              if (guid == "ui_sound") {
                s_IsSoundInContent = true;
              }
              foreach (ExportResInfo info in s_UIAtlasesInfo) {
                if (guid == info.m_Guid) {
                  s_IsShaderInContent = true;
                  s_AtlasAssetsContent.Add(info.m_AssetName);
                }
              }
            }
            data.m_IsNeedBuild = true;
          }
        }
      } else if (restype == "ui_shader") {
        SearchUIShader();
        s_IsShaderInContent = true;
        s_UIShaderInfo.m_IsNeedBuild = true;
      } else if (restype == "ui_font") {
        SearchUIFont();
        s_IsFontInContent = true;
        s_UIFontInfo.m_IsNeedBuild = true;
      } else if (restype == "ui_sound") {
        SearchUISound();
        s_IsSoundInContent = true;
        s_UISoundInfo.m_IsNeedBuild = true;
      } else if (restype == "ui_atlases") {
        SearchUIShader();
        SearchAssets("ui_atlases");
        if (Selection.objects != null && Selection.objects.Length > 0) {
          foreach (UnityEngine.Object obj in Selection.objects) {
            string selectedFile = AssetDatabase.GetAssetPath(obj);
            if (selectedFile != null && !string.IsNullOrEmpty(selectedFile.Trim())) {
              if (File.Exists(selectedFile)) {
                s_IsShaderInContent = true;
                s_AtlasAssetsContent.Add(selectedFile);
                if (tExportResDict.ContainsKey(selectedFile.ToLower())) {
                  ExportResInfo info = tExportResDict[selectedFile.ToLower()];
                  if (info != null) {
                    info.m_IsNeedBuild = true;
                  }
                }
              }
            }
          }
        }
      } else if (restype == "ui_texture") {
        SearchAssets("ui_texture");
        if (Selection.objects != null && Selection.objects.Length > 0) {
          foreach (UnityEngine.Object obj in Selection.objects) {
            string selectedFile = AssetDatabase.GetAssetPath(obj);
            if (selectedFile != null && !string.IsNullOrEmpty(selectedFile.Trim())) {
              if (File.Exists(selectedFile)) {
                s_TextureAssetsContent.Add(selectedFile);
                if (tExportResDict.ContainsKey(selectedFile.ToLower())) {
                  ExportResInfo info = tExportResDict[selectedFile.ToLower()];
                  if (info != null) {
                    info.m_IsNeedBuild = true;
                  }
                }
              }
            }
          }
        }
      }

      // 5.Export all res
      ExportAsConfig();

      ExportConfig.Log("ExportRes_UI.ExportSelectedResources Success");
      return true;
    }
    private static bool InitEnvironment()
    {
      ExportConfig.Log("--------------------------------------------------------------");
      ExportConfig.Log("ExportRes_UI Start...");

      s_AtlasAssetsContent.Clear();
      s_UIAssetsContent.Clear();
      s_IsFontInContent = false;
      s_IsSoundInContent = false;
      s_IsShaderInContent = false;

      s_UIFontInfo = null;
      s_UISoundInfo = null;
      s_UIShaderInfo = null;
      s_UIInfo.Clear();
      s_UIAtlasesInfo.Clear();

      string outputPath = ExportConfig.GetPlatformPath();
      if (!System.IO.Directory.Exists(outputPath)) {
        System.IO.Directory.CreateDirectory(outputPath);
      }
      if (!System.IO.Directory.Exists(outputPath)) {
        ExportConfig.Log("InitEnvironment create output:" + outputPath);
        return false;
      }

      ExportConfig.s_ExportResDict.Clear();
      return true;
    }
    private static bool SearchUIFont()
    {
      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;
      if (tExportResDict.ContainsKey(s_UIFontResName)) {
        ExportConfig.Log("SearchUIFont info exist:" + s_UIFontResName);
        s_UIFontInfo = tExportResDict[s_UIFontResName];
        return true;
      }
      s_UIFontInfo = new ExportResInfo();
      s_UIFontInfo.m_Id = ExportConfig.GenNextResId();
      s_UIFontInfo.m_AssetName = s_UIFontResName;
      s_UIFontInfo.m_AssetShortName = s_UIFontResName;
      s_UIFontInfo.m_PackageName = s_UIFontZipFile;
      s_UIFontInfo.m_MD5 = "";
      s_UIFontInfo.m_Size = 0;
      s_UIFontInfo.m_Guid = "";
      s_UIFontInfo.m_DependGuid = new HashSet<string>();
      s_UIFontInfo.m_IsNeedBuild = false;
      s_UIFontInfo.m_Type = s_UIFontResName;
      tExportResDict.Add(s_UIFontInfo.m_AssetName, s_UIFontInfo);
      return true;
    }
    private static bool SearchUISound()
    {
      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;
      if (tExportResDict.ContainsKey(s_UISoundResName)) {
        ExportConfig.Log("SearchUISound info exist:" + s_UISoundResName);
        s_UISoundInfo = tExportResDict[s_UISoundResName];
        return true;
      }
      s_UISoundInfo = new ExportResInfo();
      s_UISoundInfo.m_Id = ExportConfig.GenNextResId();
      s_UISoundInfo.m_AssetName = s_UISoundResName;
      s_UISoundInfo.m_AssetShortName = s_UISoundResName;
      s_UISoundInfo.m_PackageName = s_UISoundZipFile;
      s_UISoundInfo.m_MD5 = "";
      s_UISoundInfo.m_Size = 0;
      s_UISoundInfo.m_Guid = "";
      s_UISoundInfo.m_DependGuid = new HashSet<string>();
      s_UISoundInfo.m_IsNeedBuild = false;
      s_UISoundInfo.m_Type = s_UISoundResName;
      tExportResDict.Add(s_UISoundInfo.m_AssetName, s_UISoundInfo);
      return true;
    }
    private static bool SearchUIShader()
    {
      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;
      if (tExportResDict.ContainsKey(s_UIShaderResName)) {
        ExportConfig.Log("SearchUIShader info exist:" + s_UIShaderResName);
        s_UIShaderInfo = tExportResDict[s_UIShaderResName];
        return true;
      }
      s_UIShaderInfo = new ExportResInfo();
      s_UIShaderInfo.m_Id = ExportConfig.GenNextResId();
      s_UIShaderInfo.m_AssetName = s_UIShaderResName;
      s_UIShaderInfo.m_AssetShortName = s_UIShaderResName;
      s_UIShaderInfo.m_PackageName = s_UIShaderZipFile;
      s_UIShaderInfo.m_MD5 = "";
      s_UIShaderInfo.m_Size = 0;
      s_UIShaderInfo.m_Guid = "";
      s_UIShaderInfo.m_DependGuid = new HashSet<string>();
      s_UIShaderInfo.m_IsNeedBuild = false;
      s_UIShaderInfo.m_Type = s_UIShaderResName;
      tExportResDict.Add(s_UIShaderInfo.m_AssetName, s_UIShaderInfo);
      return true;
    }
    internal static bool SearchAssets(string resType)
    {
      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;

      List<ExportTargetInfo> targetList = ExportConfig.SearchExportTargetInfo(resType);
      if (targetList != null && targetList.Count > 0) {
        foreach (ExportTargetInfo info in targetList) {
          DirectoryInfo tTargetDir = new DirectoryInfo(info.m_TargetDir);
          if (!tTargetDir.Exists) {
            ExportConfig.Log("ExportRes_SceneSearchTree dir not exist:" + info.m_TargetDir);
            continue;
          }

          string[] tResBuildPattern = info.m_TargetParrern.Split(ExportConfig.s_ConfigSplit, StringSplitOptions.RemoveEmptyEntries);
          if (tResBuildPattern == null || tResBuildPattern.Length == 0) {
            ExportConfig.Log("ExportRes_SceneSearchTree pattern error:" + info.m_TargetParrern);
            continue;
          }
          FileInfo[] tTargetFiles = tResBuildPattern
            .SelectMany(i => tTargetDir.GetFiles(i, SearchOption.AllDirectories))
            .Distinct().ToArray();

          if (tTargetFiles != null && tTargetFiles.Length > 0) {
            foreach (FileInfo curFileInfo in tTargetFiles) {
              string assetPath = ExportConfig.FormatResourceName(curFileInfo.FullName);
              if (string.IsNullOrEmpty(assetPath)) {
                continue;
              }
              string assetPathLower = assetPath.ToLower();
              if (tExportResDict.ContainsKey(assetPathLower)) {
                ExportConfig.Log("GenBuildConfig GenResBuildDataByFile exist:" + assetPath);
                continue;
              }
              ExportResInfo data = new ExportResInfo();
              data.m_Id = ExportConfig.GenNextResId();
              data.m_AssetName = assetPathLower;
              data.m_AssetShortName = ExportConfig.FormatAssetShortName(assetPath);
              data.m_PackageName = ExportConfig.FormatPackageName(data.m_AssetName);
              data.m_Size = 0;
              data.m_MD5 = string.Empty;
              data.m_IsNeedBuild = false;
              data.m_Guid = AssetDatabase.AssetPathToGUID(assetPath);
              if (resType == "ui") {
                FormatUIDependency(assetPathLower, out data.m_DependGuid, out data.m_LoadDependGuid);
                s_UIInfo.Add(data);
              } else {
                data.m_DependGuid = ExportConfig.FormatDependencySet(assetPath);
                s_UIAtlasesInfo.Add(data);
              }
              data.m_Type = resType;
              tExportResDict.Add(assetPathLower, data);
            }
          }
        }
      }
      return true;
    }
    private static bool ExportSolo(string resType, ExportResInfo info)
    {
      List<UnityEngine.Object> targetAssets = new List<UnityEngine.Object>();
      List<string> targetAssetsNames = new List<string>();
      UnityEngine.Object asset = null;
      try {
        // 1.Collect all assets
        List<string> s_AssetList = ExportConfig.FormatContent(resType);

        // 2.Add assets to list
        foreach (string assetFile in s_AssetList) {
          asset = AssetDatabase.LoadAssetAtPath(assetFile, typeof(UnityEngine.Object)) as UnityEngine.Object;
          if (asset == null) {
            ExportConfig.Log("ExportSoloUIRes rename miss:" + assetFile);
            continue;
          }
          targetAssets.Add(asset);
          targetAssetsNames.Add(assetFile);
        }

        // 3.Build sheet assets
        UnityEngine.Object[] assets = targetAssets.ToArray();
        string[] assetNames = targetAssetsNames.ToArray();
        string resZipFile = ExportConfig.GetTargetPath(info);
        string pDir = Path.GetDirectoryName(resZipFile);
        if (!Directory.Exists(pDir)) {
          Directory.CreateDirectory(pDir);
        }
        if (assets != null && assets.Length > 0) {
          BuildPipeline.BuildAssetBundleExplicitAssetNames(
            assets,
            assetNames,
            resZipFile,
            ExportConfig.s_BuildOptionRes,
            ExportConfig.s_BuildOptionTarget);
          ExportConfig.Log("ExportSoloUIRes Build AB:" + info.m_PackageName);

          // 4.Zip packages
          if (ExportConfig.s_BuildOptionZip) {
            ZipHelper.ZipFile(resZipFile, resZipFile);
          }
          //info.m_IsNeedBuild = true;
        } else {
          ExportConfig.Log("ExportSoloUIRes package failed:" + info.m_PackageName);
        }
      } catch (System.Exception ex) {
        ExportConfig.Log("ExportSoloUIRes parse failedPath:" + ex);
        return false;
      } finally {
      }
      ExportConfig.Log("ExportSoloUIRes Success");
      return true;
    }
    private static bool ExportUnion(string resType, HashSet<string> assetContent, bool isUseStack)
    {
      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;

      string pathName = string.Empty;
      foreach (string resPath in assetContent) {
        ExportResInfo data = tExportResDict[resPath.ToLower()];
        if (data != null) {
          try {
            if (isUseStack) {
              BuildPipeline.PushAssetDependencies();
            }

            pathName = ExportConfig.GetTargetPath(data);
            string pDir = Path.GetDirectoryName(pathName);
            if (!Directory.Exists(pDir)) {
              Directory.CreateDirectory(pDir);
            }

            UnityEngine.Object targetAssets = AssetDatabase.LoadMainAssetAtPath(data.m_AssetName);
            UnityEngine.Object[] assets = { targetAssets };
            string[] assetNames = { data.m_AssetName };
            if (targetAssets != null) {
              BuildPipeline.BuildAssetBundleExplicitAssetNames(
                assets,
                assetNames,
                pathName,
                ExportConfig.s_BuildOptionRes,
                ExportConfig.s_BuildOptionTarget);
              if (ExportConfig.s_BuildOptionZip) {
                ZipHelper.ZipFile(pathName, pathName);
              }
              //data.m_IsNeedBuild = true;
            }
          } catch (System.Exception ex) {
            ExportConfig.Log("ExportUnion ExportResInfo failed ex:" + ex);
          } finally {
            if (isUseStack) {
              BuildPipeline.PopAssetDependencies();
            }
          }
        } else {
          ExportConfig.Log("ExportUnion ExportResInfo not exist:" + resPath);
        }
      }

      ExportConfig.Log("ExportRes_UI.ExportUnion Success");
      return true;
    }
    internal static bool ExportAsConfig()
    {
      // 1.ReExport all atlases
      BuildPipeline.PushAssetDependencies();
      if (s_IsFontInContent) {
        ExportSolo("ui_font", s_UIFontInfo);
      }
      if (s_IsSoundInContent) {
        ExportSolo("ui_sound", s_UISoundInfo);
      }
      if (s_IsShaderInContent) {
        ExportSolo("ui_shader", s_UIShaderInfo);
      }
      BuildPipeline.PushAssetDependencies();
      if (s_AtlasAssetsContent != null && s_AtlasAssetsContent.Count > 0) {
        ExportUnion("ui_atlases", s_AtlasAssetsContent, false);
      }
      if (s_TextureAssetsContent != null && s_TextureAssetsContent.Count > 0) {
        ExportUnion("ui_texture", s_TextureAssetsContent, false);
      }
      BuildPipeline.PushAssetDependencies();
      if (s_UIAssetsContent != null && s_UIAssetsContent.Count > 0) {
        ExportUnion("ui", s_UIAssetsContent, true);
      }
      BuildPipeline.PopAssetDependencies();
      BuildPipeline.PopAssetDependencies();
      BuildPipeline.PopAssetDependencies();

      // 2.ReExport all atlases
      BuildPipeline.PushAssetDependencies();
      if (s_IsShaderInContent) {
        ExportSolo("ui_shader", s_UIShaderInfo);
      }
      if (s_AtlasAssetsContent != null && s_AtlasAssetsContent.Count > 0) {
        ExportUnion("ui_atlases", s_AtlasAssetsContent, true);
      }
      BuildPipeline.PopAssetDependencies();

      // 3.Export Res List
      if (!ExportRes_ResList.Export(true)) {
        ExportConfig.Log("ExportClientResources.ExportRes_ResList failed:");
        return false;
      }

      return true;
    }
    internal static void FormatUIDependency(string assetpath, out HashSet<string> allDenpendencySet, out string loadDependency)
    {
      Dictionary<string, ExportResInfo> tExportResDict = ExportConfig.s_ExportResDict;
      HashSet<string> allLoadDenpendencySet = new HashSet<string>();
      loadDependency = string.Empty;

      string[] allDenpendeycyList = AssetDatabase.GetDependencies(new string[] { assetpath });
      allDenpendencySet = ExportConfig.FormatDependencySet(assetpath);

      if (allDenpendeycyList != null && allDenpendeycyList.Length > 0) {
        HashSet<string> allInfoDependencySet = null;
        foreach (string curDependPath in allDenpendeycyList) {
          string curDependGuid = AssetDatabase.AssetPathToGUID(curDependPath);
          if (ExportConfig.CheckAssetType(curDependPath, "ui_atlases")
            && tExportResDict.ContainsKey(curDependPath.ToLower())) {
            ExportResInfo info = tExportResDict[curDependPath.ToLower()];
            allInfoDependencySet = ExportConfig.FormatDependencySet(info.m_AssetName);
            if (allInfoDependencySet != null && allInfoDependencySet.Count > 0) {
              allDenpendencySet.ExceptWith(allInfoDependencySet);
            }
            allLoadDenpendencySet.Add(info.m_Guid);
          } else if (ExportConfig.CheckAssetType(curDependPath, "ui_texture")
            && tExportResDict.ContainsKey(curDependPath.ToLower())) {
            ExportResInfo info = tExportResDict[curDependPath.ToLower()];
            allInfoDependencySet = ExportConfig.FormatDependencySet(info.m_AssetName);
            if (allInfoDependencySet != null && allInfoDependencySet.Count > 0) {
              allDenpendencySet.ExceptWith(allInfoDependencySet);
            }
            allLoadDenpendencySet.Add(info.m_Guid);
          } else if (ExportConfig.CheckAssetType(curDependPath, "ui_shader")) {
            allDenpendencySet.Remove(curDependGuid);
            allLoadDenpendencySet.Add("ui_shader");
          } else if (ExportConfig.CheckAssetType(curDependPath, "ui_sound")) {
            allDenpendencySet.Remove(curDependGuid);
            allLoadDenpendencySet.Add("ui_sound");
          } else if (ExportConfig.CheckAssetType(curDependPath, "ui_font")) {
            allDenpendencySet.Remove(curDependGuid);
            allLoadDenpendencySet.Add("ui_font");
          }
        }
      }
      allDenpendencySet.Add(AssetDatabase.AssetPathToGUID(assetpath));

      foreach (string curDependGuid in allLoadDenpendencySet) {
        loadDependency += curDependGuid + ";";
      }
    }
    #region Menu Item
    [MenuItem("Export/Export Selected/3.UI Res/Export Client UI", false, 51)]
    internal static void ExportClientUI()
    {
      if (ExportConfig.Load()
        && ExportClientResources()) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportClientUI Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportClientUI Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/3.UI Res/Export Selected Sound", false, 53)]
    internal static void ExportSelectedSound()
    {
      if (ExportConfig.Load()
        && ExportSelectedResources("ui_sound")) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedSound Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedSound Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/3.UI Res/Export Selected Font", false, 53)]
    internal static void ExportSelectedFont()
    {
      if (ExportConfig.Load()
        && ExportSelectedResources("ui_font")) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedFont Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedFont Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/3.UI Res/Export Selected Atlases", false, 54)]
    internal static void ExportSelectedAtlases()
    {
      if (ExportConfig.Load()
        && ExportSelectedResources("ui_atlases")) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedAtlases Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedAtlases Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/3.UI Res/Export Selected Texture", false, 55)]
    internal static void ExportSelectedTexture()
    {
      if (ExportConfig.Load()
        && ExportSelectedResources("ui_texture")) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedTexture Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedTexture Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/3.UI Res/Export Selected UI", false, 56)]
    internal static void ExportSelectedUI()
    {
      if (ExportConfig.Load()
        && ExportSelectedResources("ui")) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedUI Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportSelectedUI Failed!",
          "OK");
      }
    }
    [MenuItem("Assets/Export Selected/3.UI Res/Export Client UI", false, 51)]
    internal static void Assets_ExportClientUI()
    {
      ExportClientUI();
    }
    [MenuItem("Assets/Export Selected/3.UI Res/Export Selected Sound", false, 53)]
    internal static void Assets_ExportSelectedSound()
    {
      ExportSelectedSound();
    }
    [MenuItem("Assets/Export Selected/3.UI Res/Export Selected Font", false, 53)]
    internal static void Assets_ExportSelectedFont()
    {
      ExportSelectedFont();
    }
    [MenuItem("Assets/Export Selected/3.UI Res/Export Selected Atlases", false, 54)]
    internal static void Assets_ExportSelectedAtlases()
    {
      ExportSelectedAtlases();
    }
    [MenuItem("Assets/Export Selected/3.UI Res/Export Selected Texture", false, 55)]
    internal static void Assets_ExportSelectedTexture()
    {
      ExportSelectedTexture();
    }
    [MenuItem("Assets/Export Selected/3.UI Res/Export Selected UI", false, 56)]
    internal static void Assets_ExportSelectedUI()
    {
      ExportSelectedUI();
    }
    #endregion
  }
}
